package cmw.com.beijingnew.fragment;

import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.apkfuns.logutils.LogUtils;

import cmw.com.base.BaseFragment;
import cmw.com.base.BasePager;
import cmw.com.beijingnew.R;
import cmw.com.beijingnew.activity.MainActivity;
import cmw.com.beijingnew.pager.NewsCenterPager;
import cmw.com.utils.DensityUtil;

/**
 * 主页面左侧菜单Fragment
 * Created by Administrator on 2017/4/3.
 */

public class LeftmenuFragment extends BaseFragment {
    private ListView listView = null;
    private JSONArray datas = null;
    private int prevIndex = 0;
    @Override
    public View initView() {
        LogUtils.i("左侧菜单Fragment 视图初始化完成...");
        listView = new ListView(context);
        listView.setPadding(0, DensityUtil.dip2px(context,40),0,0);//设置具顶部40 dp 填充
        listView.setDividerHeight(0);//将ListView的边框线设为0，就可以不显示边框
        listView.setCacheColorHint(Color.TRANSPARENT);//将ListView按下去默认的灰色设为透明
        listView.setSelector(android.R.color.transparent);//将ListView Item 选中时的灰色去掉，使其按下后不变色

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // 1.记录点击的Item
                prevIndex = i;
                // notifyDataSetChanged 方法执行后会先触发Adapter 的 getCount --> getView 方法
                ((BaseAdapter)listView.getAdapter()).notifyDataSetChanged();
                //2.隐藏 leftmenu菜单
                MainActivity mainActivity = (MainActivity)context;
                mainActivity.toggle();// 关 <-> 开 如果当前是开，则关；否则，是打开
                //3.切换到对应的新闻详情页面
                switchPager(i);
            }
        });
        return listView;
    }

    private void switchPager(int i) {
        MainActivity mainActivity = (MainActivity)context;
        ContentFragment contentFragment = mainActivity.getContentFragment();
        NewsCenterPager newsCenterPager = (NewsCenterPager) contentFragment.getNewsCenterPager();
        newsCenterPager.switchPager(i);
    }

    @Override
    public void initData() {
        super.initData();
        LogUtils.i("左侧菜单Fragment 数据初始化完成...");
    }

    public void setData(JSONArray datas){
        LogUtils.e("导航菜单长度="+datas.size());
        for(int i=0,count = datas.size(); i<count; i++){
           LogUtils.e("title="+datas.get(i));
        }
        this.datas = datas;
        listView.setAdapter(new LeftmenuListViewAdapter());
        switchPager(prevIndex);
    }

    /**
     * ListView Adapter 类
     */
    class LeftmenuListViewAdapter extends BaseAdapter {


        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            TextView textView = (TextView) View.inflate(context, R.layout.item_leftmenu, null);
            String title = datas.getString(i);
            textView.setText(title);
            boolean enabled = false;
            if(prevIndex == i) enabled = true;
            textView.setEnabled(enabled);
            return textView;
        }
    }
}
